package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.model.domain.User;
import com.example.mybatisplus.mapper.UserMapper;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id_user",user.getIdUser())
                .eq("password_user",user.getPasswordUser());
        User one = userMapper.selectOne(wrapper);
        if(one != null){
            SessionUtils.saveCurrentUserUpdate(one);
        }
        return one;
    }

    @Override
    public Page<User> pageList(User user, PageDTO dto) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(user.getNameUser())){
            wrapper.like("name_user",user.getNameUser());
        }
        if(Objects.nonNull(user.getIdUser())){
            wrapper.eq("id_user",user.getIdUser());
        }
        if(Objects.nonNull(user.getAuthorizationUser())){
            wrapper.eq("authorization_user",user.getAuthorizationUser());
        }
        Page<User> page = new Page<>();
        page.setCurrent(dto.getPageNo());
        page.setSize(dto.getPagesize());
        baseMapper.selectPage(page,wrapper);
        return page;
    }
}
